package com.txunda.zhuohired.fgt.user;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ants.theantsgo.config.Settings;
import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.imageLoader.GlideImageLoader;
import com.ants.theantsgo.tool.ToolKit;
import com.ants.theantsgo.util.CompressionUtil;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.aty.user.resume.EducationAty;
import com.txunda.zhuohired.aty.user.resume.JobHistoryAty;
import com.txunda.zhuohired.aty.user.resume.PersonalAty;
import com.txunda.zhuohired.aty.user.resume.PreviewAty;
import com.txunda.zhuohired.aty.user.resume.ProjectAty;
import com.txunda.zhuohired.aty.user.resume.PurposeAty;
import com.txunda.zhuohired.aty.user.resume.SelfAppraisalAty;
import com.txunda.zhuohired.aty.user.resume.SocialAty;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.user.resume.ResumeBean;
import com.txunda.zhuohired.dialog.ResumeFullDialog;
import com.txunda.zhuohired.http.pst.ResumePst;

import java.io.File;
import java.util.ArrayList;

import cn.gavinliu.android.lib.shapedimageview.ShapedImageView;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/2/26 14:57
 * 功能描述：简历页
 * 联系方式：jiahuanchen@126.com
 */

public class ResumeFgt extends BaseFgt {
    /**
     * 头像
     */
    @ViewInject(R.id.iv_resume_head)
    private ShapedImageView iv_resume_head;
    /**
     * 个人信息
     */
    @ViewInject(R.id.tv_resume_personal)
    private TextView tv_resume_personal;
    /**
     * 求职意向
     */
    @ViewInject(R.id.tv_resume_purpose)
    private TextView tv_resume_purpose;
    /**
     * 工作实习经历
     */
    @ViewInject(R.id.tv_resume_job_history)
    private TextView tv_resume_job_history;
    /**
     * 教育经历
     */
    @ViewInject(R.id.tv_resume_education)
    private TextView tv_resume_education;
    /**
     * 项目经验
     */
    @ViewInject(R.id.tv_resume_project)
    private TextView tv_resume_project;
    /**
     * 社交主页
     */
    @ViewInject(R.id.tv_resume_social)
    private TextView tv_resume_social;
    /**
     * 自我评价
     */
    @ViewInject(R.id.tv_resume_self_appraisal)
    private TextView tv_resume_self_appraisal;

    private File file;
    private int size = 0;
    private ResumePst resumePst;
    private String token = "";

    @Override
    @OnClick({R.id.ll_resume_full, R.id.iv_resume_head, R.id.ll_resume_personal, R.id.ll_resume_purpose, R.id
            .ll_resume_job_history, R.id.ll_resume_education, R.id.ll_resume_project, R.id.ll_resume_social, R.id
            .ll_resume_self_appraisal, R.id.tv_layout_resume_head_preview})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_resume_full://简历完整度
                ResumeFullDialog resumeFullDialog = new ResumeFullDialog(getActivity(), new ResumeFullDialog
                        .ResumeFullListener() {


                    @Override
                    public void onResumeFull(String type) {

                    }
                });
                resumeFullDialog.show();
                break;
            case R.id.iv_resume_head://头像
                startActivityForResult(ImageGridActivity.class, null, 100);
                break;
            case R.id.ll_resume_personal://个人信息
                startActivity(PersonalAty.class, null);
                break;
            case R.id.ll_resume_purpose://求职意向
                startActivity(PurposeAty.class, null);
                break;
            case R.id.ll_resume_job_history://工作/实习经历
                startActivity(JobHistoryAty.class, null);
                break;
            case R.id.ll_resume_education://教育经历
                startActivity(EducationAty.class, null);
                break;
            case R.id.ll_resume_project://项目经验
                startActivity(ProjectAty.class, null);
                break;
            case R.id.ll_resume_social://社交主页
                startActivity(SocialAty.class, null);
                break;
            case R.id.ll_resume_self_appraisal://自我评价
                startActivity(SelfAppraisalAty.class, null);
                break;
            case R.id.tv_layout_resume_head_preview://预览
                startActivity(PreviewAty.class, null);
                break;
        }
    }

    @Override
    protected void immersionInit() {
        showStatusBar(R.id.layout_resume_head);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_resume;
    }

    @Override
    protected void initialized() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());
        imagePicker.setShowCamera(true);// 显示相机
        imagePicker.setMultiMode(false);
        imagePicker.setCrop(true);
        imagePicker.setFocusHeight(Settings.displayWidth);
        imagePicker.setFocusWidth(Settings.displayWidth);
        imagePicker.setOutPutX(Settings.displayWidth);
        imagePicker.setOutPutY(Settings.displayWidth);
        size = ToolKit.dip2px(getActivity(), 68);
        token = application.getUserInfo().get("data");
        resumePst = new ResumePst(this);
    }

    @Override
    protected void requestData() {

    }

    /**
     * 当Fragment显示是运行该方法
     *
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {

        if (hidden) {
            //界面不可见
        } else {
            // 界面可见
            initHttp();
            //            car_status = "2";
        }
    }

    private void initHttp() {
        resumePst.resume(token);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker
                        .EXTRA_RESULT_ITEMS);
                String pic_path = CompressionUtil.compressionBitmap(images.get(0).path);
                switch (requestCode) {
                    case 100:
                        file = new File(pic_path);
                        Glide.with(this).load(file).override(size, size).centerCrop().error(R.mipmap.icon_user_head)
                                .placeholder(R.mipmap.icon_user_head).into(iv_resume_head);
                        if (file != null && file.exists())
                            resumePst.headPicIn(token, file);
                        break;
                }
            } else {
                Toast.makeText(getActivity(), "加载出错", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("resume")) {
            //            L.e("=====resume=====", jsonStr);
            ResumeBean resumeBean = GsonUtil.GsonToBean(jsonStr, ResumeBean.class);
            Glide.with(getActivity()).load(resumeBean.getData().getHead_pic()).error(R.mipmap.icon_defult)
                    .placeholder(R.mipmap.icon_defult).dontAnimate().into(iv_resume_head);
            if ("1".equals(resumeBean.getData().getInfo_status())) {
                tv_resume_personal.setText("完善");
                tv_resume_personal.setTextColor(getResources().getColor(R.color.home_grey));
            } else {
                tv_resume_personal.setText("未完善");
                tv_resume_personal.setTextColor(getResources().getColor(R.color.blue));
            }
            if ("1".equals(resumeBean.getData().getJob_status())) {
                tv_resume_purpose.setText("完善");
                tv_resume_purpose.setTextColor(getResources().getColor(R.color.home_grey));
            } else {
                tv_resume_purpose.setText("未完善");
                tv_resume_purpose.setTextColor(getResources().getColor(R.color.blue));
            }
            if ("1".equals(resumeBean.getData().getPractice_status())) {
                tv_resume_job_history.setText("完善");
                tv_resume_job_history.setTextColor(getResources().getColor(R.color.home_grey));
            } else {
                tv_resume_job_history.setText("未完善");
                tv_resume_job_history.setTextColor(getResources().getColor(R.color.blue));
            }
            if ("1".equals(resumeBean.getData().getEducation_status())) {
                tv_resume_education.setText("完善");
                tv_resume_education.setTextColor(getResources().getColor(R.color.home_grey));
            } else {
                tv_resume_education.setText("未完善");
                tv_resume_education.setTextColor(getResources().getColor(R.color.blue));
            }
            if ("1".equals(resumeBean.getData().getJobs_status())) {
                tv_resume_project.setText("完善");
                tv_resume_project.setTextColor(getResources().getColor(R.color.home_grey));
            } else {
                tv_resume_project.setText("未完善");
                tv_resume_project.setTextColor(getResources().getColor(R.color.blue));
            }
            if ("1".equals(resumeBean.getData().getSocial_status())) {
                tv_resume_social.setText("完善");
                tv_resume_social.setTextColor(getResources().getColor(R.color.home_grey));
            } else {
                tv_resume_social.setText("未完善");
                tv_resume_social.setTextColor(getResources().getColor(R.color.blue));
            }
            if ("1".equals(resumeBean.getData().getSelf_status())) {
                tv_resume_self_appraisal.setText("完善");
                tv_resume_self_appraisal.setTextColor(getResources().getColor(R.color.home_grey));
            } else {
                tv_resume_self_appraisal.setText("未完善");
                tv_resume_self_appraisal.setTextColor(getResources().getColor(R.color.blue));
            }

            return;
        }
        if (requestUrl.contains("headPicIn")) {
            initHttp();
            return;
        }
    }
}
